package com.example.bili.view.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bili.R;
import com.example.bili.allbean.FoundBean;
import com.example.bili.base.BaseFragment;
import com.example.bili.module.OkHttp;
import com.example.bili.view.activity.LiveActivity;
import com.google.gson.Gson;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.Request;

import static com.example.bili.internet.connectors.FOUND;


/**
 * Created by 杨云天 on 2017/3/12. 发现
 */

public class foundpage extends BaseFragment implements OkHttp.DataCallBack {


    @InjectView(R.id.y_tags_layout)
    TagFlowLayout yTagsLayout;
    @InjectView(R.id.y_hide_tags_layout)
    TagFlowLayout yHideTagsLayout;
    @InjectView(R.id.y_hide_scroll_view)
    NestedScrollView yHideScrollView;
    @InjectView(R.id.y_tv_more)
    TextView yTvMore;
    @InjectView(R.id.y_tquanzi)
    TextView yTquanzi;
    @InjectView(R.id.y_huati)
    TextView yHuati;
    @InjectView(R.id.y_huodong)
    TextView yHuodong;
    @InjectView(R.id.y_paihang)
    TextView yPaihang;

    private boolean isShowMore = true;

    private FoundBean foundBean;
    private List<String> tags = Arrays.asList("人渣的本愿", "极乐净土", "吃货木下",
            "暴走大事件", "阴阳师", "三生三世十里桃花",
            "主播炸了", "主播真会玩", "敖厂长",
            "鬼怪", "大胃王密子君", "一年生",
            "谷阿莫"
    );

    @Override
    protected void initview(View view) {
        TagFlowLayout tagFlowLayout = (TagFlowLayout) view.findViewById(R.id.y_tags_layout);
        tagFlowLayout.setAdapter(new TagAdapter<String>(tags) {

            @Override
            public View getView(FlowLayout parent, int position, String s) {

                TextView mTags = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tags_layout, parent, false);
                mTags.setText(s);

                return mTags;
            }
        });

    }


    @Override
    protected void m_onCreateView() {
        //请求网络
        getinternerdata();
    }

    private void getinternerdata() {
        //ok请求本类实现
        OkHttp.getAsync(FOUND, this);
    }

    @Override
    public View getLayoutview() {
        return View.inflate(getActivity(), R.layout.foundpage, null);
    }


    @Override
    public void requestFailure(Request request, IOException e) {
        Toast.makeText(getActivity(), "网络请求失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void requestSuccess(String result) throws Exception {
        //Gson转换
        Gson gson = new Gson();
        foundBean = gson.fromJson(result, FoundBean.class);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    //点击事件
    @OnClick(R.id.y_more_layout)
    public void onClick() {


        if (isShowMore) {
            isShowMore = false;
            yHideScrollView.setVisibility(View.VISIBLE);
            yTvMore.setText("收起");
            yTagsLayout.setVisibility(View.VISIBLE);
            Drawable upDrawable = getResources().getDrawable(R.drawable.ic_arrow_up_gray_round);
            upDrawable.setBounds(0, 0, upDrawable.getMinimumWidth(), upDrawable.getMinimumHeight());
            yTvMore.setCompoundDrawables(upDrawable, null, null, null);
        } else {
            isShowMore = true;
            yHideScrollView.setVisibility(View.GONE);
            yTvMore.setText("查看更多");
            yTagsLayout.setVisibility(View.VISIBLE);
            Drawable downDrawable = getResources().getDrawable(R.drawable.ic_arrow_down_gray_round);
            downDrawable.setBounds(0, 0, downDrawable.getMinimumWidth(), downDrawable.getMinimumHeight());
            yTvMore.setCompoundDrawables(downDrawable, null, null, null);

        }
    }

    //点击相关条目进入相关的页面
    @OnClick({R.id.y_tquanzi, R.id.y_huati, R.id.y_huodong, R.id.y_paihang})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.y_tquanzi:
                Intent intent1 = new Intent(getContext(), LiveActivity.class);
                startActivity(intent1);
                break;
            case R.id.y_huati:
                Intent intent2 = new Intent(getContext(), LiveActivity.class);
                startActivity(intent2);
                break;
            case R.id.y_huodong:
                Intent intent3 = new Intent(getContext(), LiveActivity.class);
                startActivity(intent3);
                break;
            case R.id.y_paihang:
                Toast.makeText(getActivity(), "您点击了原创排行榜", Toast.LENGTH_SHORT).show();
            break;
        }
    }

}
